/*
 * Copyright 2012-2023 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.spring.start.site.extension.dependency.mybatis;

import io.spring.initializr.generator.buildsystem.Dependency;
import io.spring.initializr.generator.buildsystem.DependencyContainer;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
import io.spring.initializr.generator.language.Language;
import io.spring.initializr.generator.project.MutableProjectDescription;
import io.spring.initializr.generator.spring.build.BuildCustomizer;
import io.spring.initializr.generator.version.Version;
import io.spring.initializr.generator.version.VersionParser;
import io.spring.initializr.generator.version.VersionRange;
import io.spring.initializr.generator.version.VersionReference;

import java.util.Set;

import static io.spring.initializr.generator.buildsystem.DependencyScope.ANNOTATION_PROCESSOR;
import static io.spring.initializr.generator.buildsystem.DependencyScope.COMPILE;

/**
 * mybatis-flex自定义构建器
 *
 * @author jinghong
 */
public class MyBatisFlexGradleBuildCustomizer implements BuildCustomizer<GradleBuild> {

	private static final VersionRange SPRING_BOOT_3_0_0_OR_LATER = VersionParser.DEFAULT.parseRange("3.0.0-M1");

	private final MutableProjectDescription projectDescription;

	public MyBatisFlexGradleBuildCustomizer(MutableProjectDescription projectDescription) {
		this.projectDescription = projectDescription;
	}

	@Override
	public void customize(GradleBuild build) {
		Version platformVersion = projectDescription.getPlatformVersion();
		Language language = projectDescription.getLanguage();
		String id = language.id();
		DependencyContainer dependencies = build.dependencies();
		Dependency dependency = dependencies.get("mybatis-flex");
		if ("kotlin".equals(id)) {
			build.plugins().add("org.jetbrains.kotlin.kapt", (plugin) -> plugin.setVersion("1.9.0"));
			dependencies.add("mybatis-flex-processor",
					Dependency.withCoordinates("com.mybatis-flex", "mybatis-flex-processor")
						.version(dependency.getVersion())
						.scope(ANNOTATION_PROCESSOR));
		}
		if (SPRING_BOOT_3_0_0_OR_LATER.match(platformVersion)) {
			Set<Dependency.Exclusion> exclusions = dependency.getExclusions();
			exclusions.add(new Dependency.Exclusion("org.mybatis", "mybatis-spring"));

			dependencies.add("mybatis-flex-processor",
					Dependency.withCoordinates("org.mybatis", "mybatis-spring")
						.version(VersionReference.ofValue("3.0.1"))
						.scope(COMPILE));
		}
	}

}
